<template>
    <div class="site-config">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="站点Logo" prop="site_logo">
                <el-upload class="avatar-uploader" :action="uploadAction" :headers="uploadHeaders"
                    :show-file-list="false" :on-success="handleLogoSuccess" :before-upload="beforeLogoUpload">
                    <img v-if="form.site_logo" :src="form.site_logo" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                </el-upload>
                <div class="upload-tip">建议尺寸：200x60px，支持 JPG/PNG 格式</div>
            </el-form-item>
            
            <el-form-item label="站点图标" prop="site_favicon">
                <el-upload class="favicon-uploader" :action="uploadAction" :headers="uploadHeaders"
                    :show-file-list="false" :on-success="handleFaviconSuccess" :before-upload="beforeFaviconUpload">
                    <img v-if="form.site_favicon" :src="form.site_favicon" class="favicon" />
                    <el-icon v-else class="favicon-uploader-icon"><Plus /></el-icon>
                </el-upload>
                <div class="upload-tip">建议尺寸：32x32px，支持 ICO/PNG 格式</div>
            </el-form-item>
            
            <el-form-item label="默认头像" prop="default_avatar">
                <el-upload class="avatar-uploader" :action="uploadAction" :headers="uploadHeaders"
                    :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="form.default_avatar" :src="form.default_avatar" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                </el-upload>
                <div class="upload-tip">建议尺寸：120x120px，支持 JPG/PNG 格式</div>
            </el-form-item>
            
            <el-form-item label="版权信息" prop="copyright">
                <el-input v-model="form.copyright" type="textarea" :rows="2" 
                    placeholder="请输入版权信息" style="width: 400px" />
            </el-form-item>
            
            <el-form-item label="统计代码" prop="analytics_code">
                <el-input v-model="form.analytics_code" type="textarea" :rows="4" 
                    placeholder="请输入网站统计代码" style="width: 400px" />
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="handleSubmit" :loading="loading">保存配置</el-button>
                <el-button @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const loading = ref(false)
const formRef = ref()

// 上传配置
const uploadAction = `${import.meta.env.VITE_API_BASE_URL || 'http://localhost:7001'}/upload/system-image`
const uploadHeaders = {
    token: localStorage.getItem('adminToken') || ''
}

const form = reactive({
    site_logo: '',
    site_favicon: '',
    default_avatar: '',
    copyright: 'Copyright © 2024 集美大学校友平台 版权所有',
    analytics_code: ''
})

const rules = {
    copyright: [
        { required: true, message: '请输入版权信息', trigger: 'blur' }
    ]
}

const handleLogoSuccess = (response) => {
    if (response.code === 0) {
        form.site_logo = response.data.url
        ElMessage.success('Logo上传成功')
    } else {
        ElMessage.error(response.msg || 'Logo上传失败')
    }
}

const handleFaviconSuccess = (response) => {
    if (response.code === 0) {
        form.site_favicon = response.data.url
        ElMessage.success('站点图标上传成功')
    } else {
        ElMessage.error(response.msg || '站点图标上传失败')
    }
}

const handleAvatarSuccess = (response) => {
    if (response.code === 0) {
        form.default_avatar = response.data.url
        ElMessage.success('默认头像上传成功')
    } else {
        ElMessage.error(response.msg || '默认头像上传失败')
    }
}

const beforeLogoUpload = (file) => {
    const isJPGOrPNG = file.type === 'image/jpeg' || file.type === 'image/png'
    const isLt1M = file.size / 1024 / 1024 < 1

    if (!isJPGOrPNG) {
        ElMessage.error('Logo只能是 JPG/PNG 格式!')
        return false
    }
    if (!isLt1M) {
        ElMessage.error('Logo大小不能超过 1MB!')
        return false
    }
    return true
}

const beforeFaviconUpload = (file) => {
    const isICOOrPNG = file.type === 'image/x-icon' || file.type === 'image/png'
    const isLt500K = file.size / 1024 < 500

    if (!isICOOrPNG) {
        ElMessage.error('站点图标只能是 ICO/PNG 格式!')
        return false
    }
    if (!isLt500K) {
        ElMessage.error('站点图标大小不能超过 500KB!')
        return false
    }
    return true
}

const beforeAvatarUpload = (file) => {
    const isJPGOrPNG = file.type === 'image/jpeg' || file.type === 'image/png'
    const isLt2M = file.size / 1024 / 1024 < 2

    if (!isJPGOrPNG) {
        ElMessage.error('头像只能是 JPG/PNG 格式!')
        return false
    }
    if (!isLt2M) {
        ElMessage.error('头像大小不能超过 2MB!')
        return false
    }
    return true
}

const handleSubmit = async () => {
    if (!formRef.value) return

    try {
        await formRef.value.validate()
        loading.value = true
        
        // 模拟保存配置
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        ElMessage.success('配置保存成功')
    } catch (error) {
        if (error.errors) {
            ElMessage.error('请完善表单信息')
        } else {
            ElMessage.error('保存配置失败')
        }
    } finally {
        loading.value = false
    }
}

const handleReset = () => {
    Object.keys(form).forEach(key => {
        form[key] = ''
    })
    form.copyright = 'Copyright © 2024 集美大学校友平台 版权所有'
}
</script>

<style scoped>
.site-config {
    padding: 20px;
}

.avatar-uploader {
    :deep(.el-upload) {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
        width: 200px;
        height: 60px;
    }

    :deep(.el-upload:hover) {
        border-color: var(--el-color-primary);
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 200px;
        height: 60px;
        text-align: center;
        line-height: 60px;
    }

    .avatar {
        width: 200px;
        height: 60px;
        display: block;
        object-fit: contain;
    }
}

.favicon-uploader {
    :deep(.el-upload) {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
        width: 32px;
        height: 32px;
    }

    :deep(.el-upload:hover) {
        border-color: var(--el-color-primary);
    }

    .favicon-uploader-icon {
        font-size: 18px;
        color: #8c939d;
        width: 32px;
        height: 32px;
        text-align: center;
        line-height: 32px;
    }

    .favicon {
        width: 32px;
        height: 32px;
        display: block;
        object-fit: contain;
    }
}

.upload-tip {
    font-size: 12px;
    color: #909399;
    margin-top: 8px;
}
</style>